//package com.lianxi.controller;
//
//
//import com.google.gson.Gson;
//import com.lianxi.model.CheckDto;
//import com.qiniu.common.QiniuException;
//import com.qiniu.http.Response;
//import com.qiniu.storage.Configuration;
//import com.qiniu.storage.UploadManager;
//import com.qiniu.storage.model.DefaultPutRet;
//import com.qiniu.storage.persistent.FileRecorder;
//import com.qiniu.util.Auth;
//import com.qiniu.util.StringMap;
//import lombok.Builder;
//import org.apache.commons.lang3.StringUtils;
//import org.junit.Test;
//import org.springframework.stereotype.Controller;
//
//import javax.servlet.http.HttpServletResponse;
//import javax.validation.constraints.NotBlank;
//import com.qiniu.storage.Region;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.URL;
//import java.nio.file.Paths;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.ResourceBundle;
//
//import static com.lianxi.util.StringUtil.getCutOutString;
//
//@Controller
//public class UploadController {
//
//    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("config");
//
//
//
//    public DefaultPutRet uploadVideo(InputStream inputStream){
//        //构造一个带指定 Region 对象的配置类
//        Configuration cfg = new Configuration(Region.region2());
//
////        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
////        cfg.resumableUploadMaxConcurrentTaskCount = 2;  // 设置分片上传并发，1：采用同步上传；大于1：采用并发上传
////...其他参数参考类注释
////...生成上传凭证，然后准备上传
//        String accessKey = BUNDLE.getString("qiniu.ACCESS_KEY");
//        String secretKey = BUNDLE.getString("qiniu.SECRET_KEY");
//        String bucket = BUNDLE.getString("qiniu.bucketname");
////如果是Windows情况下，格式是 D:\\qiniu\\test.png
////        String localFilePath = "E:\\Desktop\\f5f01f8f310a850d3d4a83798a83db2a.mp4";
////默认不指定key的情况下，以文件内容的hash值作为文件名
//        String key = null;
//        Auth auth = Auth.create(accessKey, secretKey);
//        String upToken = auth.uploadToken(bucket);
//        String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
//        DefaultPutRet putRet=null;
//        try {
//            //设置断点续传文件进度保存目录
//            FileRecorder fileRecorder = new FileRecorder(localTempDir);
//            UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
//            try {
//                Response response = uploadManager.put(inputStream, key, upToken,null,null);
//                //解析上传成功的结果
//                putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//                System.out.println(putRet.key);
//                System.out.println(putRet.hash);
//            } catch (QiniuException ex) {
//                Response r = ex.response;
//                System.err.println(r.toString());
//                try {
//                    System.err.println(r.bodyString());
//                } catch (QiniuException ex2) {
//                    //ignore
//                }
//            }
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }
//
//
//
//        return putRet;
//    };
//
//
//    public DefaultPutRet uploadImage(InputStream inputStream) {
//        //构造一个带指定 Region 对象的配置类
//        Configuration cfg = new Configuration(Region.region2());
////...其他参数参考类注释
//        UploadManager uploadManager = new UploadManager(cfg);
////...生成上传凭证，然后准备上传
//        String accessKey = BUNDLE.getString("qiniu.ACCESS_KEY");
//        String secretKey = BUNDLE.getString("qiniu.SECRET_KEY");
//        String bucket = BUNDLE.getString("qiniu.bucketname");
////如果是Windows情况下，格式是 D:\\qiniu\\test.png
////        String localFilePath = "E:\\Desktop\\9541.png";
////默认不指定key的情况下，以文件内容的hash值作为文件名
//
//        String key = "images/45646d1fsdf.png";
//        Auth auth = Auth.create(accessKey, secretKey);
//        String upToken = auth.uploadToken(bucket);
//        DefaultPutRet putRet=null;
//        try {
//            Response response = uploadManager.put(inputStream, key, upToken,null,null);
//            //解析上传成功的结果
//           putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//
//        } catch (QiniuException ex) {
//            Response r = ex.response;
//            System.err.println(r.toString());
////            return null;
//        }
//        return putRet;
//    }
//
//
//    @PostMapping("/upload-image")
//    public String uploadTo(MultipartFile file) {
//        DefaultPutRet putRet=null;
//        try {
//            putRet = uploadImage(file.getInputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return putRet.toString();
//    }
//
//    @PostMapping("/upload-video")
//    @ResponseBody
//    public String uploadVideo(MultipartFile file) {
//        DefaultPutRet putRet=null;
//        try {
//            putRet = uploadVideo(file.getInputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return putRet.hash;
//    }
//
//    @PostMapping("/checkcontent")
//    @ResponseBody
//    public CheckDto kkk(@NotBlank(message = "新闻内容不能为空") String content){
//        if(StringUtils.isBlank(content)){
//            return new CheckDto(500,"没有参数",null);
//        }
//        String result = wjCheck(content);
//        if(StringUtils.isBlank(result)){
//            return new CheckDto(500,"服务器异常",null);
//        }
//        String cutOutString = getCutOutString(result, "<body>", "</body>");
//        int errorNumber = Integer.valueOf(getCutOutString(result, "<ErrorCount>", "</ErrorCount>"));
//        CheckDto.CheckData data=new CheckDto.CheckData();
//        ArrayList<CheckDto.CheckResult> CheckResult = new ArrayList<>();
//        if(errorNumber==0){
//            data.setCheckResult(CheckResult);
//            return   new CheckDto(1000,"没有错误",data);
//        }else {
//            CheckDto.CheckResult checkResult = new CheckDto.CheckResult();
//            CheckResult.add(checkResult);
//            data.setCheckResult(CheckResult);
//            data.setCheckResultStr(cutOutString);
//            return new CheckDto(1000,"存在错误",data);
//        }
//    }
//
//
//
//}